/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package task02bt02;

import java.io.*;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tanhung
 */
public class Bai03 {
    private int timSoNgauNhien()
    {
        Random rd = new Random();
        return rd.nextInt();
    }
    public void run() throws IOException
    {
        String sDuongDanLuu;
        int[] mangNgauNhien = new int[10];
        // lay danh sach so ngau nhien
        for(int i = 0; i < 10; i++)
        {
            mangNgauNhien[i] = this.timSoNgauNhien();
        }
        
        FileOutputStream fos = null;        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            
            /**
             * Thu hien luu file
             */
            System.out.println("Nhap duong dan muon luu file");
            sDuongDanLuu = br.readLine();
            fos = new FileOutputStream(sDuongDanLuu);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(mangNgauNhien);
            
            System.out.println("Luu tap tin thanh cong !");
            /**
             * Thuc hien doc file
             */
            System.out.println("Du lieu cua ban da luu xuong file la:");
            
            FileInputStream fis = new FileInputStream(sDuongDanLuu);
            ObjectInputStream ois = new ObjectInputStream(fis);
            int[] ketQuaLuu;
            
            try {
                ketQuaLuu = (int[])(ois.readObject());
                for(int i = 0; i < 10; i++)
                {
                    System.out.print(String.format("%d; ",ketQuaLuu[i]));
                } 
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Bai03.class.getName()).log(Level.SEVERE, null, ex);
            }   
            
        } catch (IOException ex) {
            Logger.getLogger(Bai03.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(fos != null)
            {
                fos.close();
            }
        }
    }
}
